import { Injectable, Injector } from "@angular/core";
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from "@angular/common/http";
import { Observable } from "../../../../node_modules/rxjs";
import { RefreshTokenService } from "../services/refreshToken.service";
import { Router } from "@angular/router";

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  constructor(private injector: Injector, private router: Router) { }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {    
    const tokenService = this.injector.get(RefreshTokenService);
    if(tokenService.allowedCalls(request.url))
      return next.handle(request);
    else 
      return next.handle(tokenService.setAuthHeader(request));
  }
}